//
//  HBCategoryListCell.m
//  HeartBeat
//
//  Created by karong on 2023/9/18.
//

#import "HBCategoryListCell.h"
#import "HBCategoryModel.h"
#import "HBCategoryCell.h"
#import "HBEqualSpaceFlowLayoutEvolve.h"
@interface HBCategoryListCell ()
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) HBCategoryModel * categoryModel;
@end

@implementation HBCategoryListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    HBEqualSpaceFlowLayoutEvolve * flowLayout  = [[HBEqualSpaceFlowLayoutEvolve alloc]initWthType:HBAlignWithLeft];
    flowLayout.betweenOfCell = 0;
    flowLayout.inset = 12;
    flowLayout.minimumLineSpacing = 9;
    self.listView.collectionViewLayout = flowLayout;
}

- (void)setDataWithObject:(id)object {
    self.categoryModel = object;
    [self.listView reloadData];
    if (self.categoryModel.classesH <= 70) {
        dispatch_async(dispatch_get_main_queue(), ^{
            // 需要在主线程执行的代码
            if ([self.delegate respondsToSelector:@selector(categoryListCellRefreshHeight:)]) {
                [self.delegate categoryListCellRefreshHeight:self.listView.contentSize.height];
            }
        });
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = 0;
    count = [self.categoryModel arrayWithSection:section].count;
    return count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBCategoryCell * categoryCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBCategoryCell" forIndexPath:indexPath];
    if (indexPath.section == 2) {
        [categoryCell setPay_typeDataWithObject:[self.categoryModel arrayWithSection:indexPath.section][indexPath.row] select:[self.categoryModel isSelectIndexPath:indexPath]];
    }
    else {
        [categoryCell setOtherDataWithObject:[self.categoryModel arrayWithSection:indexPath.section][indexPath.row] select:[self.categoryModel isSelectIndexPath:indexPath]];
    }
    return categoryCell;
}


#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    HBCategoryItemModel * categoryItemModel = [self.categoryModel arrayWithSection:indexPath.section][indexPath.row];
    CGFloat width = [categoryItemModel.title boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14 weight:(UIFontWeightMedium)]} context:nil].size.width;
    size.width = width + ((indexPath.row == 0)?22:20);
    size.height = 26;
    return size;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets inset = UIEdgeInsetsZero;
    inset.left = 12;
    inset.right = 12;
    inset.bottom = 9;
    return inset;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (![self.categoryModel isSelectIndexPath:indexPath]) {
        [self.categoryModel didSelectIndexPath:indexPath];
        if ([self.delegate respondsToSelector:@selector(categoryListCellDidSelectItemAtIndexPath:)]) {
            [self.delegate categoryListCellDidSelectItemAtIndexPath:indexPath];
        }
    }
    [self.listView reloadData];
}

@end
